import allure
import jsonpath
import requests


# DS_999订单支付
def test01():
    # 01 执行登录获取token
    data = {
        "accounts": "hu2374145",
        "pwd": "hu2374145",
        "type": "username"
    }
    headers = {
        "content-type": "application/json"
    }
    params = {
        "application": "app",
        "application_client_type": "weixin"
    }
    url = "http://shop-xo.hctestedu.com/index.php?s=api/user/login"
    res_login = requests.post(url=url,headers=headers,json=data,params=params)
    token_list = jsonpath.jsonpath(res_login.json(),'$..token')
    token = token_list[0]
    print("token",token)

    # 02 加入购物车
    data = {
        "goods_id": "10",
        "spec": "",
        "stock": 1
    }
    headers = {
        "content-type": "application/json"
    }
    params = {
        "application": "app",
        "application_client_type": "weixin",
        "token":token
    }
    url = "http://shop-xo.hctestedu.com/index.php?s=api/cart/save"
    res_join_cart = requests.post(url=url,params=params,headers=headers,json=data)
    print("加入购物车",res_join_cart.text)

    # 03 查询购物车
    params = {
        "application": "app",
        "application_client_type": "weixin",
        "token":token
    }
    url = "http://shop-xo.hctestedu.com/index.php?s=api/cart/index"
    res_cart = requests.get(url=url,params=params)
    cartid_list=jsonpath.jsonpath(res_cart.json(),"$..id")
    cartid = cartid_list[0]
    print("购物车订单id",cartid)
    goodsid_list = jsonpath.jsonpath(res_cart.json(), "$..goods_id")
    goods_id = goodsid_list[0]
    print("商品id",goods_id)


    # 04 查看地址
    params = {
        "application": "app",
        "application_client_type": "weixin",
        "token": token
    }
    url = "http://shop-xo.hctestedu.com/index.php?s=api/useraddress/index"
    res_address = requests.post(url=url, params=params)
    address_list = jsonpath.jsonpath(res_address.json(), "$.data.data[0].id")
    address_id=address_list[0]
    print("地址id",address_id)

    # 04 提交订单
    data = {
        "buy_type": "cart",
        "ids": cartid,
        "payment_id": 4,
        "site_model": 0,
        "is_points": 0,
        "address_id":address_id,
        'goods_id': goods_id
    }
    params = {
        "application": "app",
        "application_client_type": "weixin",
        "token": token
    }
    url = "http://shop-xo.hctestedu.com/index.php?s=api/buy/add"
    res_cart = requests.post(url=url, params=params,json=data)
    print(res_cart.text)
    orderid_list = jsonpath.jsonpath(res_cart.json(), "$..order_ids")
    # print("提交订单id",orderid_list)
    order_id = orderid_list[0][0]
    print("提交订单id",order_id)

    # 05 订单支付
    data = {
        "ids": order_id,
        "address_id": address_id,
        "payment_id": 4
    }
    params = {
        "application": "app",
        "application_client_type": "weixin",
        "token": token
    }
    url = "http://shop-xo.hctestedu.com/index.php?s=api/order/pay"
    res_order_pay = requests.post(url=url, params=params,json=data)
    orderid_list = jsonpath.jsonpath(res_order_pay.json(), "$..order_id")
    print(res_order_pay.text)
    # print(orderid_list)
    orderid = orderid_list[0]
    print(orderid)












